/*****************************************************************************
 * plum_fsm.h
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/
#ifndef __PLUM_FSM_H__
#define __PLUM_FSM_H__

/*****************************************************************************
 * Included Files
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/** fsm section name */
#define PLUM_SERVICE_SECTION plum_srv
#define PLUM_ATTR_SECTION    plum_atr

/*****************************************************************************
 * Public Types
 ****************************************************************************/
typedef struct {
    /** device name. */
    PLUM_CONST plum_s8 *dev_name;

    /** list service info */
    plum_s32 (*info)(plum_void);

    /** device service handler. */
    plum_s32 (*service_handler)(plum_u32 srv_id, plum_s8 *argv[]);
} PLUM_SECTION_ALIGNMENT plum_fsm_srv_t;

typedef struct {
    /** attr id */
    plum_u32 id;

    /** attr space */
    plum_u32 size;
} PLUM_SECTION_ALIGNMENT plum_fsm_atr_t;

typedef plum_void (*evt_notice_t)(plum_u32 evt_id);

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/** 状态机section定义 **/
PLUM_SECTION_DEF(PLUM_SERVICE_SECTION, plum_fsm_srv_t);
PLUM_SECTION_DEF(PLUM_ATTR_SECTION, plum_fsm_atr_t);

/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/**
 * @brief:  状态机注册服务接口
 *
 * @param[in]:  dev_name    -   驱动设备名字
 * @param[in]： info        -   查询设备所支持的服务
 * @param[in]:  handle      -   调用设备服务
 * **/
#define PLUM_CAL_FSM_SERVICE_REGISTER(dev_name, info, handle) \
    PLUM_SECTION_ITEM_REGISTER(PLUM_SERVICE_SECTION,          \
                               plum_fsm_srv_t srv_##info) = { \
        (PLUM_CONST plum_s8 *)(dev_name), info, handle}

/**
 * @brief:  状态机注属性接口
 *
 * @param[in]:  id      -   属性
 * @param[in]： size    -   属性占用内存宽度
 * **/
#define PLUM_CAL_FSM_ATTR_REGISTER(id, size)                                   \
    PLUM_SECTION_ITEM_REGISTER(PLUM_ATTR_SECTION, plum_fsm_atr_t atr_##id) = { \
        id, size}

/**
 * @brief:  推送事件
 *
 * @param[in]:  evt_id      -   事件ID
 *
 * @return:
 * **/
plum_void plum_cal_fsm_evt_publish(plum_u32 evt_id);

/**
 * @brief:  订阅事件
 *
 * @param[in]:  evt_tb      -   事件ID集
 * @param[in]:  cnt         -   事件ID数量
 * @param[in]:  cb          -   事件通知回调
 *
 * @return: 成功    -   返回事件ID
 *          失败    -   返回错误码
 * **/
plum_s32 plum_cal_fsm_evt_subcribe(evt_notice_t cb, plum_u32 *evt_tb,
                                   plum_u32 cnt);

/**
 * @brief:  查询设备支持的服务
 *
 * @param[in]:  dev_name    -   设备名称
 *
 * @return: 返回执行状态
 * **/
plum_s32 plum_cal_fsm_srv_info(plum_s8 *dev_name);

/**
 * @brief:  调用设备服务
 *
 * @param[in]:  dev_name    -   设备名称
 * @param[in]:  srv_id      -   服务ID
 * @param[in]:  argv        -   服务参数集
 *
 * @return: 返回执行状态
 * **/
plum_s32 plum_cal_fsm_srv_call(plum_s8 *dev_name, plum_u32 srv_id,
                               plum_s8 **argv);

/**
 * @brief:  读设备属性
 *
 * @param[in]:  atr_id      -   属性ID
 * @param[out]: arg         -   属性值指针
 *
 * @return: 返回执行状态
 * **/
plum_s32 plum_cal_fsm_attr_read(plum_u32 atr_id, plum_void *arg);

/**
 * @brief:  写设备属性
 *
 * @param[in]:  atr_id      -   属性ID
 * @param[in]:  arg         -   参数指针
 *
 * @return: 返回执行状态
 * **/
plum_s32 plum_cal_fsm_attr_write(plum_u32 atr_id, plum_void *arg);

/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* __PLUM_FSM_H__ */
/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
